const loaderUtils = require("loader-utils");
function loader(buffer) {
  const { limit = 1000, name = "[contenthash].[ext]" } =
    loaderUtils.getOptions(this);
  if (buffer.byteLength >= limit) {
    var content = getFilename.call(this, buffer, name);
  } else {
    var content = toBase64(buffer);
  }

  return `module.exports = '${content}'`;
}

loader.raw = true;

function toBase64(buffer) {
  return `data:image/png;base64, ${buffer.toString("base64")}`;
}

function getFilename(buffer, name) {
  var filename =
    "assets/" +
    loaderUtils.interpolateName(this, name, {
      content: buffer,
    });

  this.emitFile(filename, buffer);
  return filename;
}

module.exports = loader;
